﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NNUserInfo : MonoBehaviour
{

    public UISprite avatar;
    public UILabel userNameLable;
    public UILabel userID;
    public UILabel userCoin;
    public UITexture wxAvatar;

    public GameObject BackBtn;
    private void Awake()
    {
        UIEventListener.Get(BackBtn).onClick = ButtonClick;
    }
    
    void ButtonClick(GameObject button)
    {
        if (button.name == "BackBtn")
        {
            Destroy(gameObject);
        }       
    }

    public void LoadUserInfo(int seatid) {
        EginUser user = EginUser.Instance;
        //EginDN dn = EginDN.Instance;
        for (int i = 0; i < user.gameUserInfoList.Count; i++) {
            if (user.gameUserInfoList[i].SeatId == seatid) {
                if(user.gameUserInfoList[i].AvatarUrl != ""){
                    WXAvatarManager._instance.GetWxAvatar(user.gameUserInfoList[i].Uid,wxAvatar,user.gameUserInfoList[i].AvatarUrl);
                    wxAvatar.gameObject.SetActive(true);
                }
                else
                {
                    avatar.spriteName = user.gameUserInfoList[i].AvatarId.ToString();
                }
                
                userNameLable.text = user.gameUserInfoList[i].Nickname;
                userID.text = user.gameUserInfoList[i].Uid.ToString();
                StartCoroutine(GetWWWFACE(wxAvatar, user.gameUserInfoList[i].AvatarUrl));
                int coin = 0;
                if (user.ControlScene == 1)
                {
                    coin= user.gameUserInfoList[i].Score;
                }
                else {
                    coin = user.gameUserInfoList[i].BagCoinCount;
                }
                string tempStr = "";
                if (Mathf.Abs(coin) <= 10000)
                {
                    tempStr = (Mathf.Abs(coin)).ToString();
                }
                else if (Mathf.Abs(coin) > 10000)
                {
                    int tempScore = Mathf.Abs(coin);
                    tempStr = tempScore / 10000 + ((((float)(tempScore % 10000) / (float)10000)).ToString("0.00")).Remove(0, 1) + "W";
                }
                userCoin.text = tempStr;
            }
        }

    }

    /// <summary>
    /// 加载微信头像
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    IEnumerator GetWWWFACE(UITexture texture, string url)
    {

        if (url != "")
        {
            WWW www = new WWW(url);
            yield return www;
            if (www.isDone)
            {
                if (www.error == null) { }
                texture.mainTexture = www.texture;

            }
            else
            {

            }
            www.Dispose();
        }

    }
}
